<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()" v-loading="loading">
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="数据库类型" prop="databaseType">
				<fast-select v-model="dataForm.databaseType" dict-type="database_type" placeholder="请选择" clearable></fast-select>
			</el-form-item>
			<el-form-item label="数据源类型" prop="dataType">
				<fast-select v-model="dataForm.dataType" dict-type="data_type" placeholder="请选择" clearable></fast-select>
			</el-form-item>
			<el-form-item v-if="dataForm.dataType == '1'" label="前置机主机ip" prop="edgeIp">
				<el-input v-model="dataForm.edgeIp" placeholder="前置机主机ip"></el-input>
			</el-form-item>
			<el-form-item v-if="dataForm.dataType == '1'" label="前置机端口" prop="edgePort">
				<el-input v-model="dataForm.edgePort" placeholder="前置机端口"></el-input>
			</el-form-item>
			<el-form-item label="主机ip" prop="databaseIp">
				<el-input v-model="dataForm.databaseIp" placeholder="主机ip"></el-input>
			</el-form-item>
			<el-form-item label="端口" prop="databasePort">
				<el-input v-model="dataForm.databasePort" placeholder="端口"></el-input>
			</el-form-item>
			<el-form-item label="库名(服务名)" prop="databaseName">
				<el-input v-model="dataForm.databaseName" placeholder="库名(服务名)"></el-input>
			</el-form-item>
			<el-form-item label="schema" prop="databaseSchema">
				<el-input v-model="dataForm.databaseSchema" placeholder="schema"></el-input>
			</el-form-item>
			<el-form-item label="用户名" prop="userName">
				<el-input v-model="dataForm.userName" placeholder="用户名(注意大小写)"></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="password">
				<el-input v-model="dataForm.password" placeholder="密码"></el-input>
			</el-form-item>
			<el-form-item label="jdbc连接串" prop="jdbcUrl">
				<!-- <el-input disabled v-model="dataForm.jdbcUrl" placeholder="jdbc连接串(若填写将以填写的内容连接,否则会后台自动构建连接)"></el-input> -->
				<el-input
					:disabled="dataForm.databaseType != 2"
					v-model="dataForm.jdbcUrl"
					placeholder="后台自动构建连接"
					:title="dataForm.jdbcUrl"
				></el-input>
			</el-form-item>
			<!-- <el-form-item>
				<el-button type="primary" @click="changeORCL()">切换</el-button>
			</el-form-item> -->
			<!-- <el-form-item label="所属项目" prop="projectId">
					<fast-project-select v-model="dataForm.projectId" placeholder="所属项目" clearable></fast-project-select>
				</el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="test()">测试</el-button>
			<el-button :loading="loading" type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useDatabaseApi, useDatabaseSubmitApi, testOnline } from '@/api/data-integrate/database'
import { ElLoading } from 'element-plus'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	name: '',
	databaseType: '',
	dataType: '',
	edgeIp: '',
	edgePort: '',
	databaseIp: '',
	databasePort: '',
	databaseName: '',
	databaseSchema: '',
	userName: '',
	password: '',
	jdbcUrl: '',
	projectId: ''
})

const loading = ref(false)

const init = (id?: number) => {
	visible.value = true
	loading.value = false
	dataForm.id = ''

	dataForm.edgeIp = ''
	dataForm.edgePort = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getDatabase(id)
	} else {
		dataForm.edgeIp = ''
		dataForm.edgePort = ''
	}
}

const getDatabase = (id: number) => {
	useDatabaseApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}
let reg_tel_phone = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	databaseType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	dataType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	edgeIp: [
		{ required: true, message: '必填项不能为空', trigger: 'blur' },
		{ pattern: reg_tel_phone, message: '前置机主机ip格式不正确', trigger: 'blur' }
	],
	edgePort: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	databaseIp: [
		{ required: true, message: '必填项不能为空', trigger: 'blur' },
		{ pattern: reg_tel_phone, message: '主机ip格式不正确', trigger: 'blur' }
	],
	databasePort: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	databaseName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	databaseSchema: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	userName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	password: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
	/* projectId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }] */
})

// const databaseChange = value => {
// 	if (value == 4) {
// 		dataForm.databaseSchema = 'dbo'
// 	}
// }

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			loading.value = false
			return false
		}

		loading.value = true
		if (dataForm.dataType != '1') {
			dataForm.edgeIp = ''
			dataForm.edgePort = ''
		}

		if (dataForm.databaseType == '2') {
			dataForm.databaseSchema = dataForm.databaseSchema.toUpperCase()
			dataForm.databaseName = dataForm.databaseName.toUpperCase()
		} else {
			dataForm.databaseSchema = dataForm.databaseSchema.toLowerCase()
			dataForm.databaseName = dataForm.databaseName.toLowerCase()
		}

		useDatabaseSubmitApi(dataForm)
			.then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						loading.value = false
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
			.catch(() => {
				loading.value = false
			})
	})
}

// 测试连接
const test = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		const testLoading = ElLoading.service({
			lock: true,
			text: '连接测试中...... 请稍后',
			background: 'rgba(0, 0, 0, 0.7)'
		})
		if (dataForm.databaseType == '2') {
			dataForm.databaseSchema = dataForm.databaseSchema.toUpperCase()
			dataForm.databaseName = dataForm.databaseName.toUpperCase()
		} else {
			dataForm.databaseSchema = dataForm.databaseSchema.toLowerCase()
			dataForm.databaseName = dataForm.databaseName.toLowerCase()
		}
		testOnline(dataForm)
			.then(() => {
				testLoading.close()
				setTimeout(() => {
					ElMessage.success({
						message: '测试连接成功',
						duration: 500,
						onClose: () => {
							emit('refreshDataList')
						}
					})
				}, 100)
			})
			.catch(() => {
				testLoading.close()
			})
	})
}

// const changeORCL = () => {
// 	if (dataForm.databaseType == '2') {
// 		dataForm.jdbcUrl = dataForm.jdbcUrl
// 	}
// }

defineExpose({
	init
})
</script>
